package com.humanminoritygame.mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailSend {
    private String to;
    private String subject;
    private String body;
    private static final String smtpServer = "smtp.gmail.com";
    private static final String smtpServerPort = "587";                  
    private static final String from = "canchitas.de.futbol@gmail.com";         
    private static final String passwd = "chicaslandia"; 

    public MailSend(String to, String subject, String body){
      this.to = to;
      this.subject = subject;
      this.body = body;
    }
    
    public void EnviarMail(){
      try {
        Properties props = new Properties();
        props.setProperty("mail.smtp.host", smtpServer);
        props.setProperty("mail.smtp.port", smtpServerPort);
        props.setProperty("mail.smtp.user", from);
        props.setProperty("mail.smtp.auth", "false");
        props.setProperty("mail.smtp.starttls.enable", "true");

        // Preparamos la sesion
        Session session = Session.getDefaultInstance(props);
        // Construimos el mensaje   
        MimeMessage message = new MimeMessage(session);                        
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));             
        message.setSubject(subject);
        message.setContent(body, "text/html"); 

        // Lo enviamos
        Transport t = session.getTransport("smtp");
        t.connect(from, passwd);
        t.sendMessage(message, message.getAllRecipients());

        // Cierre 
        t.close();
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
}
